package com.ymatou.iapi.productquery.testcase;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.ProductPriceCallService;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productquery.service.GetCatalogListByDeliveryExtraIsolationCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class Ts_GetCatalogListByDeliveryExtraIsolation {
	private static GetCatalogListByDeliveryExtraIsolationCall getCatalogListByDeliveryExtraIsolationCall = new GetCatalogListByDeliveryExtraIsolationCall();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("交易隔离查询接口, 多物流购物车中商品列表");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getCatalogListByDeliveryExtraIsolationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByDeliveryExtraIsolation_001() {
		Logger.start(true,"普通单规格商品，普通单规格直播商品 查询");
		Logger.comment("has been test in Tc_GetCatalogListByDeliveryExtra_001");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByDeliveryExtraIsolation_002() {
		Logger.start(true,"普通多规格商品,普通多规格直播商品 查询");
		Logger.comment("has been test in Tc_GetCatalogListByDeliveryExtra_002");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByDeliveryExtraIsolation_003() {
		Logger.start(true,"普通活动商品查询");
		Logger.comment("has been test in Tc_GetCatalogListByDeliveryExtra_003");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByDeliveryExtraIsolation_004() {
		Logger.start(true,"交易隔离活动商品查询");
		Logger.comment("has been test in Tc_GetCatalogListByDeliveryExtra_004");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByDeliveryExtraIsolation_005() {
		Logger.start(true,"活动直播商品查询");
		Logger.comment("has been test in Tc_GetCatalogListByDeliveryExtra_005");
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByDeliveryExtraIsolation_006() {
		Logger.start(true,"活动直播商品查询");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			Date beginTime = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(-3600));
			Date endTime = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(3600*4));
			ProductPriceCallService.ApplyHighActivity("10",productId,beginTime,endTime);
			ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			new ActivityProductsWapper().update(productId, MapUtil.hashMap("Earnest",1,"EarnestDedution",2));
			ProductManagerCallServiceV3.updateProductRedisTimeStamp(productId);
		}catch (Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByDeliveryExtraIsolation_007() {
		Logger.start(true,"活动直播商品增加发布类型");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addpro.getProduct().setPublishType(2);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			Thread.sleep(4000);
			List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(productId).stream().map(ele->ele.getsCatalogId()).collect(Collectors.toList());
			List<List<String>> catalogAndTypeList = new ArrayList<>();
			catalogs.stream().forEach(ele->catalogAndTypeList.add(Arrays.asList(ele,"0")));
			JSONObject ret = getCatalogListByDeliveryExtraIsolationCall.search(catalogAndTypeList.get(0));
			int PublishType = ret.getJSONObject("Data").getJSONArray("ProductList").getJSONObject(0).getInt("PublishType");
			Logger.verifyEquals(2, PublishType, "验证PublishType");
		}catch (Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByDeliveryExtraIsolation_008() {
		Logger.start(true,"活动直播商品增加奢侈品");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			addpro.getProduct().getCatalogList().get(0).setPrice(10000);
			addpro.getProduct().getCatalogList().get(0).setNewPrice(10000);
			addpro.getProduct().getCatalogList().get(0).setVipPrice(10000);
			addpro.getProduct().getCatalogList().get(0).setMarketPrice(18000);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Thread.sleep(3000);
			
			List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(productId).stream().map(ele->ele.getsCatalogId()).collect(Collectors.toList());
			List<List<String>> catalogAndTypeList = new ArrayList<>();
			catalogs.stream().forEach(ele->catalogAndTypeList.add(Arrays.asList(ele,"0")));
			JSONObject ret = getCatalogListByDeliveryExtraIsolationCall.search(catalogAndTypeList.get(0));
			JSONObject Product = ret.getJSONObject("Data").getJSONArray("ProductList").getJSONObject(0);
			Logger.verifyEquals(true,Product.getBoolean("IsLuxury"), "验证PublishType");
		}catch (Exception e){
			Logger.fail(e);
		}
	}
	
	
	
}
